using System;
using System.Collections.Generic;
using System.Text;
using SqlTools.DataAccessLibrary;
using SqlTools.SampleDAL.Data;
using System.Data;

namespace SqlTools.SampleDAL
{
	public partial class Address : BusinessObject, IDbObject<AddressPrimaryKey>
	{
		protected AddressPrimaryKey _addressPrimaryKey;
		protected int _customerID;
		protected Customer _customer;
		protected string _line1;
		protected string _line2;
		protected string _line3;
		protected string _city;
		protected string _state;
		protected string _postalCode;

		public Address()
		{
			_addressPrimaryKey = new AddressPrimaryKey();
			_customerID = 0;
			_customer = null;
			_line1 = "";
			_line2 = "";
			_line3 = "";
			_city = "";
			_state = "";
			_postalCode = "";
		}

		#region IDbObject<AddressPrimaryKey> Members

		public AddressPrimaryKey Key
		{
			get { return _addressPrimaryKey; }
		}

		#endregion
		
		internal int CustomerID
		{
			get { return _customerID; }
		}

		public Customer Customer
		{
			get
			{
				if ( _customer == null && _customerID != 0 )
				{
					DbCustomer dbCustomer;
					if ( !string.IsNullOrEmpty(_ConnectionStringName) )
						dbCustomer = new DbCustomer(_ConnectionStringName);
					else
						dbCustomer = new DbCustomer();

					_customer = dbCustomer.SelectByPK(new CustomerPrimaryKey(_customerID));
				}
				return _customer;
			}
			set
			{
				if ( _customer != value )
				{
					_customer = value;

					if ( _customer != null )
					{
						_customerID = _customer.Key.CustomerID;
						_customer.Inserted += delegate(object sender, BusinessObjectEventArgs e)
						{
							if ( e.Item != null )
							{
								_customerID = ( (Customer)e.Item ).Key.CustomerID;
							}
						};
					}
					else
					{
						_customerID = 0;
					}

					_UpdateState();
				}
			}
		}

		public string Line1
		{
			get { return _line1; }
			set
			{
				_line1 = value;
				_UpdateState();
			}
		}

		public string Line2
		{
			get { return _line2; }
			set
			{
				_line2 = value;
				_UpdateState();
			}
		}

		public string Line3
		{
			get { return _line3; }
			set
			{
				_line3 = value;
				_UpdateState();
			}
		}

		public string City
		{
			get { return _city; }
			set
			{
				_city = value;
				_UpdateState();
			}
		}

		public string State
		{
			get { return _state; }
			set
			{
				_state = value;
				_UpdateState();
			}
		}

		public string PostalCode
		{
			get { return _postalCode; }
			set
			{
				_postalCode = value;
				_UpdateState();
			}
		}

		public override void Update()
		{
			OnUpdating(this);

			DbAddress dbAddress = CreateDbAddress();

			dbAddress.Update(this);

			OnUpdated(this);
		}

		public override void Insert()
		{
			OnInserting(this);

			DbAddress dbAddress = CreateDbAddress();
			dbAddress.Insert(this);
			
			OnInserted(this);
		}

		public override void Delete()
		{
			OnDeleting(this);

			DbAddress dbAddress = CreateDbAddress();
			dbAddress.Delete(this);

			OnDeleted(this);
		}

		protected DbAddress CreateDbAddress()
		{
			if ( !string.IsNullOrEmpty(_ConnectionStringName) )
				return new DbAddress(_ConnectionStringName);
			else
				return new DbAddress();
		}

		public static Address CreateFromReader(IDataReader reader)
		{
			Address address = new Address();

			FillFromReader(address, reader);

			return address;
		}

		public static void FillFromReader(Address address, IDataReader reader)
		{
			address.OnLoading(address);

			address.Key.AddressID = (int)reader["AddressID"];
			address._customerID = (int)reader["CustomerID"];
			address._line1 = reader["Line1"].ToString();
			address._line2 = reader["Line2"].ToString();
			address._line3 = reader["Line3"].ToString();
			address._city = reader["City"].ToString();
			address._state = reader["State"].ToString();
			address._postalCode = reader["PostalCode"].ToString();

			address._UpdateState(ObjectState.None);

			address.OnLoaded(address);
		}

		public static Address Load(AddressPrimaryKey key)
		{
			return Load(null, key);
		}

		public static Address Load(string connName, AddressPrimaryKey key)
		{
			DbAddress dbAddress;

			if ( string.IsNullOrEmpty(connName) )
			{
				dbAddress = new DbAddress();
			}
			else
			{
				dbAddress = new DbAddress(connName);
			}

			return dbAddress.SelectByPK(key);
		}
	}
}
